﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LIB
{
    public static class trans
    {
        /// <summary>
        /// 字段是否包含
        /// </summary>
        /// <param name="strSource"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool strContain(string strSource, string key, string op = ",")
        {
            return (op + strSource + op).Contains(op + key + op);
        }
        
        public static string GetChecked(System.Web.UI.WebControls.CheckBoxList checkList, string separator = ",")
        {
            string selval = "";
            for (int i = 0; i < checkList.Items.Count; i++)
            {
                if (checkList.Items[i].Selected)
                {
                    selval += checkList.Items[i].Value + separator;
                }
            }
            return selval;
        }
        public static string SetChecked(System.Web.UI.WebControls.CheckBoxList checkList, string selval, string separator = ",")
        {
            selval = separator + selval + separator;        //例如："0,1,1,2,1"->",0,1,1,2,1,"
            for (int i = 0; i < checkList.Items.Count; i++)
            {
                checkList.Items[i].Selected = false;
                string val = separator + checkList.Items[i].Value + separator;
                if (selval.IndexOf(val) != -1)
                {
                    checkList.Items[i].Selected = true;
                    selval = selval.Replace(val, separator);        //然后从原来的值串中删除已经选中了的
                    if (selval == separator)        //selval的最后一项也被选中的话，此时经过Replace后，只会剩下一个分隔符
                    {
                        selval += separator;        //添加一个分隔符
                    }
                }
            }
            selval = selval.Substring(1, selval.Length - 2);        //除去前后加的分割符号
            return selval;
        }
        /// <summary>
        /// 代码显示样式 div+textarea
        /// </summary>
        /// <param name="code"></param>
        /// <param name="templateName"></param>
        /// <param name="tblname"></param>
        /// <returns></returns>
        public static string codeInTextarea(string code,string templateName,string tblname)
        {
            return "<div class=\"row\">" + templateName + "：" + tblname + "<br><textarea name=\"" + tblname + "\" rows=\"10\" cols=\"80\" onclick=\"oCopy(this);\">" + code+"</textarea></div>";

        }
        /// <summary>
        /// 得到扩展名
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string getExt(string fileName)
        {
            int i = fileName.IndexOf(".");
            if (i > 0) return fileName.Substring(i, fileName.Length - i);
            else return "";
        }
        /// <summary>
        /// 首字母大写
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string upperFirstChar(string str)
        {
            if (str==null || str.Length < 1) return str;
            string functionStr = str.Substring(0, 1).ToUpper() + str.Substring(1);
            return functionStr;
        }
    }

}